package com.server.Service;

import com.server.Example.MoviesExample;
import com.server.entity.Movies;
import com.server.mapper.MoviesMapper;
import org.apache.ibatis.session.SqlSession;
import untils.MybatisClass;
import untils.Utils;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.Date;
import java.util.Random;

/**
 * The type Movies service.
 * Movie service implementation class
 *
 * @author user
 */
public class MoviesService {

    /**
     * Select vales movies.
     * Information about movies to be released in the coming week
     *
     * @return String JsonList Calling method: MoviesService.nextWeekMovies()
     */
    public static ArrayList<Movies> nextWeekMovies(){
        SqlSession sqlSession = MybatisClass.getSqlSession();
        MoviesMapper movies = sqlSession.getMapper(MoviesMapper.class);
        MoviesExample moviesExample = new MoviesExample();
        Date date = new Date();
        moviesExample.createCriteria().andMovieIsShowEqualTo(true).andMovieCreateBetween(date,new Date(date.getTime() + 7 * 24 * 60 * 60 * 1000L));
        ArrayList<Movies> moviesList = (ArrayList<Movies>)movies.selectByExample(moviesExample);
        sqlSession.close();
        return moviesList;
    }

    /**
     * Select by primary key movies.
     * Get information about a single piece of data
     *
     * @param id the id
     * @return the movies Calling method: MoviesService.selectByPrimaryKey(id)
     */
    public static Movies selectByPrimaryKey(Integer id){
        SqlSession sqlSession = MybatisClass.getSqlSession();
        MoviesMapper movies = sqlSession.getMapper(MoviesMapper.class);
        Movies movie=movies.selectByPrimaryKey(id);
        sqlSession.close();
        return movie == null ?new Movies():movie;
    }

    /**
     * Instr vales movies.
     * Generate the time value in a fixed time interval and generate the value in the database
     *
     * @param number    the number
     * @param startTime the start time
     * @param entire    the entire
     * @return Returns the number of rows affected Calling method: MoviesService.instrValesMoviesMovies(number,startTime,entire)
     */
    public static int instrValesMoviesMovies(int number, String startTime, String entire){
        int sumCount = 0;
        SqlSession sqlSession=MybatisClass.getSqlSession();
        MoviesMapper movies = sqlSession.getMapper(MoviesMapper.class);
        for (int i = 0; i < number; i++) {
            sumCount+=movies.insert(new Movies(Utils.getRandomString(12)+i,
                    Utils.getRandomString(12)+i,
                    Utils.getRandomString(12)+i,
                    Utils.getRandomString(12)+i, BigDecimal.valueOf(Math.random() * (100 - 10) + 10).setScale(2, RoundingMode.DOWN),
                    new Random().nextBoolean(),
                    Utils.randomDate(startTime, entire))
            );
            sqlSession.commit();
        }
        sqlSession.close();
        return sumCount;
    }
}
